import { TBY } from '@/api/config/servicePort'
import http from '@/api'
import { ReqPage, ResPage } from '@/api/interface'

export namespace LayerConfig {
  /**查询参数 */
  export interface Query extends ReqPage {
    name?: string
    active?: number
  }

  /**列表内容 */
  export interface Record {
    id: number
    name: string
    active: number
    icon: string
    url: string
    createTimeFormat: string
  }

  /**详情 */
  export interface Form {
    id: number
    name: string
    active: number
    icon: string
    url: string
  }

  /**启用状态枚举 */
  export const ActiveEnum = [
    { value: 0, label: '停用' },
    { value: 1, label: '启用' }
  ]
}

/**分页查询 */
export const pageLayerConfig = (params: LayerConfig.Query) => {
  return http.get<ResPage<LayerConfig.Record>>(TBY + `/layer_config/page`, params)
}

// 详情
export const getLayerConfigDetail = (id: number) => {
  return http.get<Partial<LayerConfig.Form>>(TBY + `/layer_config/get`, { id }, { loading: false })
}

// 新增或修改
export const saveOrUpdateLayerConfig = (params: Partial<LayerConfig.Form>) => {
  return http.post(TBY + `/layer_config/save`, params)
}

// 删除
export const removeLayerConfig = (id: number) => {
  return http.delete(TBY + `/layer_config/delete`, { id })
}

//是否启用
export const activeLayerConfig = (id: number, active: number) => {
  let params: LayerConfig.Form = { id: id, active: active, name: '', icon: '', url: '' }
  return http.post(TBY + `/layer_config/active`, params)
}

// 获取当前配置的图层
export const getLayerConfigActive = () => {
  return http.get<Partial<LayerConfig.Form>>(TBY + `/layer_config/active_data`, { loading: false })
}

// 验证名称是否存在
export const existName = (name: string, id?: number) => {
  return http.get<boolean>(TBY + '/layer_config/exist_name', { name, id }, { loading: false })
}

// 验证地址是否存在
export const existUrl = (url: string, id?: number) => {
  return http.get<boolean>(TBY + '/layer_config/exist_url', { url, id }, { loading: false })
}
